<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15 0015
 * Time: 上午 11:04
 */
defined("IN_IA")or exit("Access denied");
global $_GPC,$_W;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"home_set";
checklogin();  //验证是否登录
if($op=='home_set'){
    $con=array(
        'uniacid'=>$uniacid,
        'ikey'=>array('home_title','bar_title','is_open_coupon','coupon_count','home_title_jump_url'),
    );
    $setData=pdo_getall('cqkundian_farm_set',$con);
    $list=array();
    foreach ($setData as $key=>$value){
        $list[$value['ikey']]=$value['value'];
    }
    include $this->template("web/home/home_set");
}

if($op=='home_set_save'){

    $data=$_POST;
    if (!$data['is_open_coupon']){
        $data['is_open_coupon']=0;
    }
    $res=0;
    foreach ($data as $key => $value){
        $update_data=array(
            'ikey'=>$key,
            'value'=>$value,
            'uniacid'=>$uniacid,
        );
        $con=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $setData=pdo_get('cqkundian_farm_set',$con);
        if(empty($setData)){
            $res+=pdo_insert('cqkundian_farm_set',$update_data);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$update_data,$con);
        }
    }
    if($res){
        message('操作成功',$this->createWebUrl('home'));
    }else{
        message("操作失败");
    }
    die;
}

//首页分类列表
if($op=='type_list'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    $listCount=pdo_getall("cqkundian_farm_home_type",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_home_type",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/home/type_list");
}
//编辑首页分类
if($op=='type_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get("cqkundian_farm_home_type",array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    include $this->template("web/home/type_edit");
}

//保存分类信息
if($op=='type_save'){
    $insertData=array(
        'type_name'=>trim($_GPC['type_name']),
        'icon'=>tomedia($_GPC['icon']),
        'rank'=>$_GPC['rank'],
        'url'=>$_GPC['url'],
        'uniacid'=>$uniacid,
        'status'=>$_GPC['status'],
    );
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_home_type',$insertData);
    }else{
        $res=pdo_update('cqkundian_farm_home_type',$insertData,array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    if($res){
        $url=url('site/entry/home',array('m'=>'kundian_farm','op'=>'type_list'));
        message('操作成功',$url);
        message('操作成功',$this->createWebUrl('home'));
    }else{
        message("操作失败");
    }
}

//分类状态更新
if($op=='statusChange'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_home_type',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}
//删除分类信息
if($op=='delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_home_type',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//新的底部信息展示
if($op=='home_btm_set_now'){
    $condition=array(
        'ikey'=>array('plate_one','plate_two','plate_three'),
        'uniacid'=>$uniacid,
    );
    $btmData=pdo_getall('cqkundian_farm_set',$condition);
    $list=array();
    foreach ($btmData as $key=>$v){
        $list[$v['ikey']]=unserialize($v['value']);
    }
    include $this->template('web/home/home_btm_set_now');
}

//保存新的底部信息
if($op=='home_btm_now_save'){
    $res=0;
    //版块一
    $one_value=array(
        'title'=>$_GPC['title1'],
        'icon'=>tomedia($_GPC['icon1']),
        'remark'=>$_GPC['remark1'],
        'jump_url'=>$_GPC['jump_url1'],
    );
    $data_one=array(
        'ikey'=>'plate_one',
        'value'=>serialize($one_value),
        'uniacid'=>$uniacid,
    );
    $cond_one=array('uniacid'=>$uniacid, 'ikey'=>'plate_one');
    //版块二
    $two_value=array(
        'title'=>$_GPC['title2'],
        'icon'=>tomedia($_GPC['icon2']),
        'remark'=>$_GPC['remark2'],
        'jump_url'=>$_GPC['jump_url2'],
    );
    $data_two=array(
        'ikey'=>'plate_two',
        'value'=>serialize($two_value),
        'uniacid'=>$uniacid,
    );
    $cond_two=array('uniacid'=>$uniacid,'ikey'=>'plate_two');
    //版块三
    $three_value=array(
        'title'=>$_GPC['title3'],
        'icon'=>tomedia($_GPC['icon3']),
        'remark'=>$_GPC['remark3'],
        'jump_url'=>$_GPC['jump_url3'],
    );
    $data_three=array(
        'ikey'=>'plate_three',
        'value'=>serialize($three_value),
        'uniacid'=>$uniacid,
    );
    $cond_three=array('uniacid'=>$uniacid, 'ikey'=>'plate_three',);
    $oneData=pdo_getall('cqkundian_farm_set',$cond_one);
    if(empty($oneData)){
        $res+=pdo_insert('cqkundian_farm_set',$data_one);
    }else{
        $res+=pdo_update('cqkundian_farm_set',$data_one,$cond_one);
    }
    $twoData=pdo_getall('cqkundian_farm_set',$cond_two);
    if(empty($twoData)){
        $res+=pdo_insert('cqkundian_farm_set',$cond_two);
    }else{
        $res+=pdo_update('cqkundian_farm_set',$data_two,$cond_two);
    }
    $threeData=pdo_getall('cqkundian_farm_set',$cond_three);
    if(empty($threeData)){
        $res+=pdo_insert('cqkundian_farm_set',$data_three);
    }else{
        $res+=pdo_update('cqkundian_farm_set',$data_three,$cond_three);
    }
    if($res){
        message('操作成功',url('site/entry/home',array('m'=>'kundian_farm','op'=>'home_btm_set_now')));
    }else{
        message('操作失败');
    }

}

//个人中心设置
if($op=='center_set'){
    $con=array(
        'uniacid'=>$uniacid,
        'ikey'=>array(
            'hide_center_land',
            'hide_center_animal',
            'hide_center_integral',
            'hide_center_group',
            'hide_center_group_order',
            'hide_center_coupon',
            'hide_center_card',
            'hide_center_cart',
            'hide_center_address',
            'hide_center_issue',
            'center_contact_type',
        ),
    );
    $setData=pdo_getall('cqkundian_farm_set',$con);
    $list=array();
    foreach ($setData as $key=>$value){
        $list[$value['ikey']]=$value['value'];
    }
    include $this->template("web/home/center_set");
}

//保存个人中心设置
if($op=='center_set_save'){
    $data=array(
        'hide_center_land'=>$_GPC['hide_center_land'] ? $_GPC['hide_center_land']:0,
        'hide_center_animal'=>$_GPC['hide_center_animal'] ? $_GPC['hide_center_animal']:0,
        'hide_center_integral'=>$_GPC['hide_center_integral'] ? $_GPC['hide_center_integral']:0,
        'hide_center_group'=>$_GPC['hide_center_group'] ? $_GPC['hide_center_group']:0,
        'hide_center_group_order'=>$_GPC['hide_center_group_order'] ? $_GPC['hide_center_group_order']:0,
        'hide_center_coupon'=>$_GPC['hide_center_coupon'] ? $_GPC['hide_center_coupon']:0,
        'hide_center_card'=>$_GPC['hide_center_card'] ? $_GPC['hide_center_card']:0,
        'hide_center_cart'=>$_GPC['hide_center_cart'] ? $_GPC['hide_center_cart']:0,
        'hide_center_address'=>$_GPC['hide_center_address'] ? $_GPC['hide_center_address']:0,
        'hide_center_issue'=>$_GPC['hide_center_issue'] ? $_GPC['hide_center_issue']:0,
        'center_contact_type'=>$_GPC['center_contact_type'],
    );
    $res=0;
    foreach ($data as $key => $value){
        $update_data=array(
            'ikey'=>$key,
            'value'=>$value,
            'uniacid'=>$uniacid,
        );
        $con=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $setData=pdo_get('cqkundian_farm_set',$con);
        if(empty($setData)){
            $res+=pdo_insert('cqkundian_farm_set',$update_data);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$update_data,$con);
        }
    }
    if($res){
        message('操作成功',url('site/entry/home',array('m'=>'kundian_farm','op'=>'center_set')));
    }else{
        message("操作失败");
    }
}


